# -*- coding: utf-8 -*-
import oss2
import os

auth = oss2.Auth("LTAI5tMYGaWniUab8XRACiba", "58PBxfMIzpuRusVlpG0MdQGnloWklX")
bucket = oss2.Bucket(
    auth,
    "http://oss-cn-guangzhou.aliyuncs.com",
    "r0site-img"
)


def get_all_img_url(prefix: str) -> list:
    # 列举文件夹下的文件与子文件夹名称，不列举子文件夹下的文件
    ans = []
    for obj in oss2.ObjectIteratorV2(bucket, prefix=prefix, delimiter="/"):
        if obj.is_prefix():
            print("directory: " + obj.key)
        else:
            tmp_key = obj.key.replace(prefix, "", 1)
            if tmp_key:
                # 返回值为链接，参数依次为，方法/oss上文件路径/过期时间(s)
                file_url = bucket.sign_url("GET", obj.key, 60*60*24)
                file_name = os.path.splitext(tmp_key)[0]
                ans.append({
                    "file_name": file_name,
                    "file_url": file_url
                })
    return ans


# print(get_all_img_url("frog/"))
